﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VSDBM.Code.dao;

public partial class Buscador : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        String filtros = " WHERE ";

        if (cbacession.Checked)
        {
            filtros += " LOWER(s.idacession) LIKE '" + tbidacession.Text.ToLower() + "' AND ";
        }

        if (cbdescription.Checked)
        {
            filtros += " LOWER(s.definition) LIKE '%" + tbdescription.Text.ToLower() + "%' AND ";
        }

        if (cborganism.Checked)
        {
            filtros += " LOWER(org.name) LIKE '%" + tborganism.Text.ToLower() + "%' AND ";
        }

        if (cbsubtype.Checked)
        {
            filtros += " LOWER(st.description) LIKE '%" + tbsubtype.Text.ToLower() + "%' AND ";
        }

        if (cbregionmap.Checked)
        {
            filtros += " LOWER(rf.description) LIKE '%" + tbfeatures.Text.ToLower() + "%' AND ";
        }
        if (cbsize.Checked)
        {
            filtros += " s.size = " + tbsize.Text;
        }

        if (!cbsize.Checked && !cbregionmap.Checked && !cbsubtype.Checked && !cborganism.Checked && !cbdescription.Checked && !cbacession.Checked)
        {
            filtros += " LOWER(s.definition) LIKE '%" + tbbusca.Text.ToLower() + "%' ";
        }

        Char[] query = filtros.ToCharArray();
        String newfiltros = "";

        for (int i = 1; i < 5; i++)
        {
            if (query[query.Length - i] == 'D')
            {
                if (query[query.Length - i - 1] == 'N')
                {

                    if (query[query.Length - i - 2] == 'A')
                    {
                        query[query.Length - i] = ' ';
                        query[query.Length - i - 1] = ' ';
                        query[query.Length - i - 2] = ' ';
                    }
                }
            }
        }

        newfiltros = new String(query);
        if (filtros.Trim() == "WHERE")
        {
            newfiltros = "";
        }

        Session["searchfilter"] = newfiltros;
        Response.Redirect("BuscadorQuery.aspx");
    }

}